import { observe } from "../src/observer";

export function initState(vm) {
    initData(vm);
}

function initData(vm) {
    let data = vm.$options.data;
    data = vm._data = typeof data === 'function' ? data.call(vm) : data || {};
    for (const key in data) {
        proxy(vm, '_data', key);
    }
    observe(vm._data);
}

function proxy(target, sourceKey, key) {
    Object.defineProperty(target, key, {
        get() {
            return target[sourceKey][key];
        },
        set(newVal) {
            if (newVal === target[sourceKey][key]) return;
            target[sourceKey][key] = newVal;
        }
    })
}